package com.sx.micowell.framework.model


import android.location.Location
import com.example.mvvm.base.data.SingleLiveEvent
import com.example.mvvm.base.vm.BaseViewModel
import com.example.mvvm.rx.RxBus
import com.example.mvvm.until.LogUtils
import com.example.mvvm.until.SPUtils
import com.example.mvvm.until.ToastUtils
import com.miloyu.modelnet.BusEventInfo

import com.miloyu.modelnet.network.ApiResponse

import com.miloyu.modelnet.network.HttpClient
import com.miloyu.modelnet.network.service.IApiService
import com.miloyu.modelnet.network.tools.RxUtils
import com.miloyu.modelnet.response.ImagePrefixVO
import com.miloyu.modelnet.response.UserInfoVO
import com.sx.micowell.framework.app.AppConstant
import com.miloyu.modelnet.response.log
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.entity.AccountBean
import com.sx.micowell.framework.tools.EventInfo
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import java.util.concurrent.ConcurrentHashMap


/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/6/25 11:46
 */
class MainModel : BaseViewModel() {

    val loginPopMap: ConcurrentHashMap<Int, SingleLiveEvent<Void>> = ConcurrentHashMap()

    // 登录过期
    var loginInvalidEvent: SingleLiveEvent<Void> = SingleLiveEvent()

    var isAuthEvent: SingleLiveEvent<Void> = SingleLiveEvent()

    var locationEvent: SingleLiveEvent<Location> = SingleLiveEvent()

    var uuid: String = ""

    init {
        getInitData()
    }

    var driverError: SingleLiveEvent<Boolean> = SingleLiveEvent()

    override fun registerRxBus() {
        super.registerRxBus()
        LogUtils.d("registerRxBus")
        addSubscribe(
            RxBus.getDefault().toObservable(BusEventInfo::class.java)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { event ->
                    if (event.type == 403) {
                        if (loginPopMap.isEmpty()) {
                            loginPopMap[0] = loginInvalidEvent
                            loginPopMap[0]?.call()
                        }
                    }
                }
        )
        addSubscribe(RxBus.getDefault().toObservable(Location::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { event ->
                locationEvent.value = event
            }
        )

    }


    /**
     * 获取初始化数据
     */
    private fun getInitData() {
        addSubscribe(
            Observable.merge(
                HttpClient.instance.apiService.getImagePrefix(),
                HttpClient.instance.apiService.userInfo()
            )
                .compose(RxUtils.compose())
                .subscribeWith(object : ApiResponse<Any>() {

                    override fun success(data: Any?, msg: String?) {
                        when (data) {
                            is ImagePrefixVO -> {
                                SPUtils.getInstance().put(SPKey.SP_IMAGE_PREFIX, data.prefix)
                            }
                            is UserInfoVO -> {
                                if (!data.isAuth) {
                                    isAuthEvent.call()
                                }
                                SPUtils.getInstance().put(SPKey.SP_VIP, data.isVip)
                            }
                        }
                    }

                    override fun onFail(e: Throwable?, errorMsg: String?) {

                    }

                })
        )
    }

    public fun checkLocation(longitude: String, latitude: String) {
        HttpClient.instance.create(IApiService::class.java)
            .checkLocation(uuid, longitude, latitude)
            .compose(RxUtils.compose())
            .subscribeWith(object : ApiResponse<Void>() {

                override fun success(data: Void?, msg: String?) {

                }

                override fun onFail(e: Throwable?, errorMsg: String?) {
                    ui.hideDialog()
                    ToastUtils.showShort("$errorMsg")
//                    driverError.call()
                }

            })
    }
}






